package com.android.task5;

import java.sql.Date;
import java.util.ArrayList;

import android.util.Log;



public class FlickrRESTRequest {
	
	private final static String key = "af132e1981a446a441e273025b302f64";
	//Flickr REST API
	private final static String baseUrl = "http://api.flickr.com/services/rest";
	
	private final static String searchTag = "flickr.photos.search";
	private final static String getInfoTag = "flickr.photos.getInfo"; //not used, for getInfo we use XML-RPC
	private final static int accuracy = 1;	
	private final static int limit = 10;
	private final static String minUploadDate = "1990-01-01";
	
	
	protected static enum Method {search, getInfo};
	private static Method currentMethod = Method.search;
	
	private final static String INFO_TAG = "Flickr Request Info";
	
	ArrayList<String> tags;
//	BoundingBox bbox;
	
	public FlickrRESTRequest(){
		tags = new ArrayList<String>();
	
	}
	
	public void setSearchMethod(){
		currentMethod = Method.search;
	}
	
	public void setGetInfoMethod(){
		currentMethod = Method.getInfo;
	}
	
	public String getCurrentMethodTag(){
		if(currentMethod == Method.search) return searchTag;
		else return getInfoTag;
	}
	
	public String createStringURLForSearchTags(ArrayList<String> searchTags){
		setSearchMethod();
		String url = baseUrl;
		url += "?";
		url += "method=";
		url += getCurrentMethodTag();
		url += "&";
		url += "format=rest";
		url += "&";
		url += "api_key=";
		url += key;
		url += "&";
		url += "accuracy=";
		url += accuracy;
		url += "&";
		url += "min_upload_date=";
		url += minUploadDate;
		url += "&";		
		url += "per_page=";
		url += limit;
		url += "&";
		url += "tags=";
		
		for(int i=0; i<searchTags.size(); i++){
			url += searchTags.get(i);
				if(i!=searchTags.size()-1)url += "+";
		}
		Log.i(INFO_TAG, url);
		return url;
	}
	
	public String createStringURLForBoundingBox(BoundingBox bbox){
		String url = baseUrl;
		url += "?";
		url += "method=";
		url += getCurrentMethodTag();
		url += "&";
		url += "format=rest";
		url += "&";
		url += "api_key=";
		url += key;
		url += "&";
		url += "accuracy=";
		url += accuracy;
		url += "&";
		url += "min_upload_date=";
		url += minUploadDate;
		url += "&";		
		url += "per_page=";
		url += limit;
		url += "&";
		url += "content_type=1&"; //for photos only
		url += "bbox=";
		
//		bbox (Optional)
//	    A comma-delimited list of 4 values defining the Bounding Box of the area that will be searched.
//
//	    The 4 values represent the bottom-left corner of the box and the top-right corner, minimum_longitude, minimum_latitude, maximum_longitude, maximum_latitude.
//
//	    Longitude has a range of -180 to 180 , latitude of -90 to 90. Defaults to -180, -90, 180, 90 if not specified. 
		url +=  bbox.getBottomLeftCornerLongitude();		
		url += ",";
		url += bbox.getBottomLeftCornerLatitude();
		url += ",";
		url += bbox.getTopRightCornerLongitude();
 		url += ",";
 		url += bbox.getTopRightCornerLatitude();
//		url += "tags=";
//		for(int i=0; i<searchTags.size(); i++){
//		url += searchTags.get(i);
//		if(i!=searchTags.size()-1)url += "+";
//		}
		Log.i(INFO_TAG, url);
		return url;
	}
}

